package com.activenetwork.smlite.business;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.schema.model.event.EventType;
import com.activenetwork.smlite.page.event.EventsHomeView;
import com.activenetwork.smlite.page.event.SmartFolder.FOLDER_LEVEL;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.page.generic.ModelPage;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.verification.VerificationAction;

public class EventBusiness {
	
	private Logger logger = LoggerFactory.getLogger(EventBusiness.class);
	/**
	 * Search the event by specific string searchContent
	 * Do not select any event, just do search
	 * @param searchContent
	 * @param actions
	 */
	public void searchEvent(String searchContent, VerificationAction...actions){
		logger.debug("Only do input search");
		EventsHomeView eventsHomeView = PageLoader.load(EventsHomeView.class);
		eventsHomeView = eventsHomeView.setSearchEvent(searchContent);
		eventsHomeView = eventsHomeView.clickSearch();
	}
	/**
	 * Search with specific string and select one event according to
	 * Data of event jaxb object
	 * @param searchContent
	 * @param event
	 * @param actions
	 */
	public void searchEvent(String searchContent, EventType event, VerificationAction...actions){
		logger.debug("Select the smart folder if necessary");
		EventsHomeView eventsHomeView = PageLoader.load(EventsHomeView.class);
		if(event.getFolder() != null){
			String lv1folder = event.getFolder().getLevel1();
			String lv2folder = event.getFolder().getLevel2();
			String lv3folder = event.getFolder().getLevel3();
			if(lv3folder.length() > 0){
				eventsHomeView = eventsHomeView.hightlightFolder(lv3folder, FOLDER_LEVEL.LEVEL3);
			}else if(lv2folder.length() > 0){
				eventsHomeView = eventsHomeView.hightlightFolder(lv2folder, FOLDER_LEVEL.LEVEL2);
			}else{
				eventsHomeView = eventsHomeView.hightlightFolder(lv1folder, FOLDER_LEVEL.LEVEL1);
			}
		}
		
		logger.debug("Do input search");
		eventsHomeView = eventsHomeView.setSearchEvent(searchContent);
		eventsHomeView = eventsHomeView.clickSearch();
		
		logger.debug("Select event from grid");
		eventsHomeView.selectEventFromGrid(event);
	}
	/**
	 * Search with the specific string and select one event accoding to
	 * the string eventTitle
	 * @param searchContent
	 * @param eventTitle
	 * @param actions
	 */
	public void searchEvent(String searchContent, String eventTitle, VerificationAction...actions){
		logger.debug("Do input search");
		EventsHomeView eventsHomeView = PageLoader.load(EventsHomeView.class);
		eventsHomeView = eventsHomeView.setSearchEvent(searchContent);
		eventsHomeView = eventsHomeView.clickSearch();
		
		logger.debug("Select event from grid");
		EventType event = new EventType();
		EventBasicType basic = new EventBasicType();
		basic.setTitle(eventTitle);
		event.setBasic(basic);
		eventsHomeView.selectEventFromGrid(event);
	}
	/**
	 * Select the smart folder and then do search the specific string for the event 
	 * @param searchContent
	 * @param eventTitle
	 * @param folderName
	 * @param level
	 */
	public void searchEvent(String searchContent, String eventTitle, String folderName, FOLDER_LEVEL level){
		logger.debug("Construct the event object");
		EventType event = new EventType();
		EventBasicType basic = new EventBasicType();
		basic.setTitle(eventTitle);
		event.setBasic(basic);
		
		logger.debug("Select the smart folder if necessary");
		EventsHomeView eventsHomeView = PageLoader.load(EventsHomeView.class);
		eventsHomeView = eventsHomeView.hightlightFolder(folderName, level);
		
		logger.debug("Do input search");
		eventsHomeView = eventsHomeView.setSearchEvent(searchContent);
		eventsHomeView = eventsHomeView.clickSearch();
		
		logger.debug("Select event from grid");
		eventsHomeView.selectEventFromGrid(event);
	}
	/**
	 * Add one event by event data type
	 * Pay attention that this method use less UI operation but sendkeys
	 * @param event
	 * @param actions
	 */
	public void addEvent(EventType event, VerificationAction...actions){
		logger.debug("Click navigation bar - event");
		ModelPage page = PageLoader.load(ModelPage.class);
		EventsHomeView eventsHomeView = (EventsHomeView)page.events();
		
		if(event.getFolder() != null){
			logger.debug("Handle the event folder");
			String level1 = event.getFolder().getLevel1();
			String level2 = event.getFolder().getLevel2();
			String level3 = event.getFolder().getLevel3();
			// delete if existing
			try{
				eventsHomeView = eventsHomeView.deleteLevel1Folder(level1);
				logger.debug("Delete for adding level1 folder " + level1);
			}catch(RuntimeException e){
				logger.debug("Level 1 folder " + level1 + " not exist");
			}
			eventsHomeView = eventsHomeView.createCascadingFolder(level1, level2, level3);
		}
		
		logger.debug("Click Add event button");
		EventBasicSubview eventBasicPage =  eventsHomeView.clickAddEvent();
		
		logger.debug("set event title");
		eventBasicPage.setEventTitle(event.getBasic());

		logger.debug("set event locaton by AJAX");
		try{
			eventBasicPage = eventBasicPage.selectEventLocationAJAX(event.getBasic().getLocation());
		}catch(RuntimeException e){
			logger.debug("Unable to select existing location. Create new.");
			eventBasicPage.setEventLocation(event.getBasic());
			eventBasicPage = eventBasicPage.saveLocation();
		}
		
		logger.debug("set event category");
		eventBasicPage.setEventCategory(event.getBasic());
		
		logger.debug("set event length");
		if(event.getBasic().getLength().getOneDay() != null){
			eventBasicPage.setSingleDate(event.getBasic());
		}else{
			eventBasicPage.setMultiDate(event.getBasic());
		}	
		
		logger.debug("set components");
		EventBasicType.Components components = event.getBasic().getComponents();
		if(components != null){
			eventBasicPage = eventBasicPage.selectComponents(components);
		}
		
		logger.debug("set event tags by AJAX");
		eventBasicPage = eventBasicPage.showEventTag();
		if(event.getBasic().getTag() != null){
			List<EventBasicType.Tag> tags = event.getBasic().getTag();
			for(EventBasicType.Tag tag : tags){
				try{
					eventBasicPage = eventBasicPage.selectEventTagAJAX(tag);
				}catch(RuntimeException e){
					logger.debug("Unable to select existing tag. Create new.");
					eventBasicPage = eventBasicPage.setTag(tag);
					eventBasicPage = eventBasicPage.saveTag();
				}

			}
		}
			
		logger.debug("Go to next step");
		eventBasicPage.nextStep();
		
		logger.info("Event has been created by addEvent()");
	}
	/**
	 * Add one event by event data type
	 * Pay attention that this method use more UI operation other than sendkeys
	 * @param event
	 * @param actions
	 */
	public void addEventUI(EventType event, VerificationAction...actions){
		logger.debug("Click navigation bar - event");
		ModelPage page = PageLoader.load(ModelPage.class);
		EventsHomeView eventsHomeView = (EventsHomeView)page.events();
		
		if(event.getFolder() != null){
			logger.debug("Handle the event folder");
			String level1 = event.getFolder().getLevel1();
			String level2 = event.getFolder().getLevel2();
			String level3 = event.getFolder().getLevel3();
			// delete if existing
			try{
				eventsHomeView = eventsHomeView.deleteLevel1Folder(level1);
				logger.debug("Delete for adding level1 folder " + level1);
			}catch(RuntimeException e){
				logger.debug("Level 1 folder " + level1 + " not exist");
			}
			eventsHomeView = eventsHomeView.createCascadingFolder(level1, level2, level3);
		}
		
		logger.debug("Click Add event button");
		EventBasicSubview eventBasicPage =  eventsHomeView.clickAddEvent();
		
		logger.debug("set event title");
		eventBasicPage.setEventTitle(event.getBasic());
		
		logger.debug("set event locaton by AJAX");
		try{
			eventBasicPage = eventBasicPage.selectEventLocationAJAX(event.getBasic().getLocation());
		}catch(RuntimeException e){
			logger.debug("Unable to select existing location. Create new.");
			eventBasicPage.setEventLocation(event.getBasic());
			eventBasicPage = eventBasicPage.saveLocation();
		}
		
		logger.debug("set event category");
		eventBasicPage.setEventCategoryUI(event.getBasic());
		
		logger.debug("set event length");
		if(event.getBasic().getLength().getOneDay() != null){
			eventBasicPage = eventBasicPage.setSingleDateUI(event.getBasic());
		}else{
			eventBasicPage = eventBasicPage.setMultiDateUI(event.getBasic());
		}		
		
		logger.debug("set components");
		EventBasicType.Components components = event.getBasic().getComponents();
		if(components != null){
			eventBasicPage = eventBasicPage.selectComponents(components);
		}
		
		logger.debug("set event tags by AJAX");
		eventBasicPage = PageLoader.load(EventBasicSubview.class);
		eventBasicPage = eventBasicPage.showEventTag();
		if(event.getBasic().getTag() != null){
			List<EventBasicType.Tag> tags = event.getBasic().getTag();
			for(EventBasicType.Tag tag : tags){
				try{
					eventBasicPage = eventBasicPage.selectEventTagAJAX(tag);
				}catch(RuntimeException e){
					logger.debug("Unable to select existing tag. Create new.");
					eventBasicPage = eventBasicPage.setTag(tag);
					eventBasicPage = eventBasicPage.saveTag();
				}

			}
		}
		
		logger.debug("Go to next step");
		eventBasicPage.nextStep();
		
		logger.info("Event has been created by addEventUI()");
	}
	/**
	 * Delete the event by searching it out first
	 * @param searchContent
	 * @param event
	 * @param actions
	 */
	public void deleteEvent(String searchContent, EventType event, VerificationAction...actions){
		logger.debug("Search out the event");
		EventsHomeView eventsHomeView = PageLoader.load(EventsHomeView.class);
		eventsHomeView = eventsHomeView.setSearchEvent(searchContent);
		eventsHomeView = eventsHomeView.clickSearch();
		
		logger.debug("Select event from grid");
		eventsHomeView = eventsHomeView.checkEventFromGrid(event);
		eventsHomeView = eventsHomeView.clickDeleteEvent();
		
		logger.debug("Kill the popup");
		eventsHomeView.confirmPopup(true);
	}
	/**
	 * Delete the event by searching its name 
	 * @param searchContent
	 * @param eventName
	 * @param actions
	 */
	public void deleteEvent(String searchContent, String eventName, VerificationAction...actions){
		EventType event = new EventType();
		EventBasicType basicType = new EventBasicType();
		basicType.setTitle(eventName);
		event.setBasic(basicType);
		deleteEvent(searchContent, event, actions);
	}
}
